package com.fairytask.ui.item
{
	import com.jzonl.config.BattleConfig;
	import com.fairytask.engine.components.BaseItem;
	import com.fairytask.engine.components.button.Button;
	import com.fairytask.engine.components.clip.Clip;
	import com.fairytask.engine.data.BaseData;
	import com.fairytask.engine.data.BindManager;
	import com.fairytask.engine.data.ObjectData;
	import com.fairytask.engine.define.Filter;
	import com.fairytask.engine.define.ResConfig;
	import com.fairytask.engine.managers.ResManager;
	import com.fairytask.engine.resource.StaticIni;
	import com.fairytask.engine.resource.loader.SwfLoader;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	
	/**
	 * 阵型中的站位抽象出来的子项
	 * @author yangj
	 * 
	 */	
	public class GridviewItem extends BaseItem
	{
				//菱形的底框
		private var _model:MovieClip;		//人物的模型或者是可以添加时闪动的箭头
		private var _flag:Boolean;			//判断是否是开启的格子
		private var _pos:int;				//存放人物或者是宠物的信息（人物代表0，宠物是宠物视图中的位置）
		private var _num:int;				//用于存储是哪个格子按钮
		private var _bindArr:Array;
		private var bindManager	:BindManager	=	BindManager.getInstance();
		private var _data:ObjectData;
		private var modelSpace:Sprite;			//用于存放模型的容器
		public var select:Boolean = false;	//是否被选中
		private var clip:Clip	=	new Clip();
		public function GridviewItem(num:int,pos:int=-2)
		{
			this.addChild(clip);
			//给变量赋值
			_pos = pos;
			_num = num;
			this.name	=	_num.toString();
			this.x = Number(StaticIni.getIniVar("GridviewPos_"+num.toString(),"x"));
			this.y = Number(StaticIni.getIniVar("GridviewPos_"+num.toString(),"y"));
			modelSpace = new Sprite();
			modelSpace.x=30;
			modelSpace.y=30;
			addChild(modelSpace);
			btnUnclick();
			this.doubleClickEnabled	=	true;
			this.mouseChildren	=	false;
			//this.addEventListener(MouseEvent.DOUBLE_CLICK,onClick);
		}
		
		private function onClick(evt:MouseEvent):void
		{
			
			trace("双击了");
			
		}
		/**
		 *绑定数据 
		 * 
		 */		
		private function initBind():void
		{
			_bindArr.push([_data,"Model",bindManager.BindProp(_data,"Model",this,"roleModel")]);		//人物模型
		}
		
		/**
		 * 设置人物模型 
		 * @param val
		 * 
		 */
		public function set roleModel(val:String):void
		{
//			SwfLoader.StartLoad(ResConfig.npcResPath+val+"_model.swf",_model,false,onLoadModel);
			SwfLoader.StartLoad(BattleConfig.BATTLE_RES+val+"_1.swf",_model,false,onLoadModel);
			
		}
		
		/**
		 * 加载人物模型 
		 * @param val
		 * @param pList
		 * 
		 */
		private function onLoadModel(val:DisplayObject,pList:Array):void
		{
			_model	=	val as MovieClip;
			if(_model)
			{
				while(modelSpace.numChildren>0)
				{
					modelSpace.removeChildAt(0);
				}
				_model.x	=	12;
				_model.y	=	3;
				var tmpScale	:Number	=	100/_model.height;
				/*_model.height	=	100;
				_model.width	=	100;*/
				_model.scaleX	=	_model.scaleY	=0.5;
				_model.gotoAndPlay(0);
				
				modelSpace.addChild(_model);
			}
		}
		/**
		 *如果能够放下人物显示的是箭头 
		 * 
		 */		
		public function canDownPerson():void
		{
//			SwfLoader.StartLoad(ResConfig.npcResPath +"1001100011_model.swf",_model,false,onLoadModel);
			_model = null;
			_model = ResManager.newResClass("gain") as MovieClip;
			_model.x	=	-5;
			_model.y	=	-70;
			modelSpace.addChild(_model);
		}
		/**
		 *设置按钮是否可以点击 
		 */		
		private function btnUnclick():void
		{
			if(_pos==-2)
			{
				clip.source	=	ResConfig.pngResPath+"lineup4.png";
				this.mouseEnabled	=	false;
			}
			else
			{
				clip.source	=	ResConfig.pngResPath+"lineup2.png";
			}
		}
		/**
		 *设置子项的图片是人物还是宠物 
		 * @param val
		 * 
		 */		
		public function set source(val:String):void
		{

			
		}
		
		/**
		 *返回第几个格子 
		 * @return 
		 * 
		 */		
		public function get num():int
		{
			return _num;
		}
		
		/**
		 *返回子项是什么
		 * @return 
		 * 
		 */		
		public function get pos():int
		{
			return  _pos;
		}
		
		/**
		 *给子项添加选中的效果 
		 * 
		 */		
		public function addSelect():void
		{
			if(select==true)
			{
				return;
			}
			clip.filters	=	[Filter.YELLOW_INNER];
			select	=	true;
		}
		
		/**
		 *删除系选中的效果 
		 */		
		public function removeSelect():void
		{
			if(select==false)
			{
				return;
			}
			
			clip.filters	=	null;
			select = false;
		}
		
		/**
		 *清除所有的数据 
		 * 
		 */		
		public function clearData():void
		{
			
			_data	=	null;
		}
		
		public function set pdata(val:*):void
		{
			_data	=	val;
			initBind();
		}
		

		
	}
}